package com.gitee.wsl.common.ui.symbol

import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Path
import com.gitee.wsl.common.ui.base.Point
import com.gitee.wsl.common.ui.symbol.generator.pi
import com.gitee.wsl.common.ui.symbol.generator.tau
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

fun Symbol.Companion.star(position: Point,  radius: Float) = Star(position, radius)

data class Star(override var position: Point, val radius: Float):LocationSymbol

private val ka = 0.8908131f
private val kr = sin(pi / 10) / sin(7 * pi / 10)
private val kx = sin(tau / 10) * kr
private val ky = -cos(tau / 10) * kr

fun Path.star(star: Star){
    val x = kx * star.radius
    val y = ky * star.radius
    moveTo(star.position.x, star.position.y - star.radius)
    lineTo(star.position.x + x,star.position.y + y)
    for (i in 1 until 5) {
        val a = tau * i / 5.0f
        val c = cos(a)
        val s = sin(a)
        lineTo(star.position.x + (s * star.radius), star.position.y - (c * star.radius))
        lineTo(star.position.x + (c * x) - (s * y), star.position.y + (s * x) + (c * y))

    }
    close()
}

fun Path.addStar(
    size: Size,
    spikes: Int = 5,
    outerRadiusFraction: Float = 0.5f,
    innerRadiusFraction: Float = 0.2f
): Path {
    val outerRadius = size.minDimension * outerRadiusFraction
    val innerRadius = size.minDimension * innerRadiusFraction

    val centerX = size.width / 2
    val centerY = size.height / 2

    var totalAngle = PI / 2 // Since we start at the top center, the initial angle will be 90°
    val degreesPerSection = (2 * PI) / spikes

    moveTo(centerX, 0f) // Starts at the top center of the bounds

    var x: Double
    var y: Double

    for (i in 1..spikes) {
        // Line going inwards from outerCircle to innerCircle
        totalAngle += degreesPerSection / 2
        x = centerX + cos(totalAngle) * innerRadius
        y = centerY - sin(totalAngle) * innerRadius
        lineTo(x.toFloat(), y.toFloat())

        // Line going outwards from innerCircle to outerCircle
        totalAngle += degreesPerSection / 2
        x = centerX + cos(totalAngle) * outerRadius
        y = centerY - sin(totalAngle) * outerRadius
        lineTo(x.toFloat(), y.toFloat())
    }

    // Path should be closed to ensure it's not an open shape
    close()

    return this
}